package com.example.swim;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;
import androidx.viewpager2.widget.ViewPager2;
import com.google.android.material.tabs.TabLayout;
import com.google.android.material.tabs.TabLayoutMediator;
import com.google.gson.Gson;

import org.jetbrains.annotations.NotNull;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import fragment.Ti_yu;
import fragment.guo_ji;
import fragment.guo_nei;
import fragment.ke_ji;
import fragment.tou_tiao;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

public class MainActivity extends AppCompatActivity {
    private OkHttpClient okhttp=new OkHttpClient();
    private List<Fragment> list=new ArrayList<>();
    private String result;
    private TabLayout tablayout;
    private ViewPager2 vp2;
    private EditText editText;
    private Button button;
    private SwipeRefreshLayout news_swipe;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
        list.add(tou_tiao.newInstance("头条",""));
        list.add(guo_nei.newInstance("国内",""));
        list.add(guo_ji.newInstance("国际",""));
        list.add(Ti_yu.newInstance("体育",""));
        list.add(ke_ji.newInstance("科技",""));
        FragmentManager fragmentManager=getSupportFragmentManager();
        Swim_vp2_Adaptet swim_vp2_adaptet=new Swim_vp2_Adaptet(fragmentManager,getLifecycle(),list);
        vp2.setAdapter(swim_vp2_adaptet);
        new TabLayoutMediator(tablayout, vp2, true, new TabLayoutMediator.TabConfigurationStrategy() {
            @Override
            public void onConfigureTab(@NonNull TabLayout.Tab tab, int position) {
                tab.setText(list.get(position).getArguments().getString("param1"));
            }
        }).attach();
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String edtext=editText.getText().toString();
                if (edtext!=null){
                    String url="https://way.jd.com/jisuapi/newSearch?keyword="+edtext+"&appkey=1eab6690b59eda53649e8ad98ca5ad03";
                    new Thread(new Runnable() {
                        @Override
                        public void run() {
                            Request request=new Request.Builder().url(url).build();
                            Call call=okhttp.newCall(request);
                            call.enqueue(new Callback() {
                                @Override
                                public void onFailure(@NotNull Call call, @NotNull IOException e) {

                                }

                                @Override
                                public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException {
                                    result=response.body().string();
                                    runOnUiThread(new Runnable() {
                                        @Override
                                        public void run() {
                                            Gson gson=new Gson();
                                            main_api api=gson.fromJson(result,main_api.class);
                                            Intent intent=new Intent(MainActivity.this,MainActivity2.class);
                                            String title=api.getResult().result.getList().get(0).getTitle();
                                            String time=api.getResult().result.getList().get(0).getTime();
                                            String content=api.getResult().result.list.get(0).getContent();
                                            intent.putExtra("title",title);
                                            intent.putExtra("time",time);
                                            intent.putExtra("content",content);
                                            startActivity(intent);
                                        }
                                    });
                                }
                            });
                        }
                    }).start();
                }
            }
        });
    }
    private void initView() {
        button=findViewById(R.id.button2);
        editText=findViewById(R.id.edittext);
        tablayout = (TabLayout) findViewById(R.id.tablayout);
        vp2 = (ViewPager2) findViewById(R.id.vp2);
        news_swipe=findViewById(R.id.news_swipe);
    }
}